package edu.harding.redzone;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OffenseModeActivity extends Activity
{
	private final int DIALOG_SAVE_BEFORE_CHANGE_ID = 1;
	private final int DIALOG_ERASE_ID = 0;
	private final int DIAGLOG_CHOOSE_ROUTE_ID = 2;
	private final int DIALOG_SAVE_ID = 3;
	private final int DIALOG_BACK_ID = 4;
	private final int OFFENSE = 0;

	private final int DRAW = 1;
	private final int MOVE = 2;
	private final int ERASE = 3;
	private final int CLEAR = 4;
	private final int BLOCK = 5;

	@SuppressWarnings("unused")
	private Button mDrawButton;
	@SuppressWarnings("unused")
	private Button mMoveButton;
	@SuppressWarnings("unused")
	private Button mEraseButton;

	private EditText mPlayNameSave;
	private Intent mIntent;
	private Toast mToast;

	private static int mMode;
	private static int mRoute;
	private Context mContext;
	protected Handler mHandler = new Handler();
	protected Utility mUtil;
	protected Play mPlay = new Play("", "", null);
	private boolean mSavedBeforeChanged;

	public static int Route()
	{
		return mRoute;
	}

	private void Route(int value)
	{
		mRoute = value;
	}

	public static int Mode()
	{
		return mMode;
	}

	public static void Mode(int value)
	{
		mMode = value;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.offense);

		mDrawButton = (Button) findViewById(R.id.buttonDraw);
		mMoveButton = (Button) findViewById(R.id.buttonMove);
		mEraseButton = (Button) findViewById(R.id.buttonErase);

		mContext = this.getApplicationContext();
		mUtil = new Utility(mContext);

		showToast(this.getString(R.string.offense), Toast.LENGTH_LONG);

		Mode(-1);
		mSavedBeforeChanged = false;
	}

	@Override
	public void onBackPressed()
	{
		showDialog(DIALOG_BACK_ID);
	}

	public void eraseButtonClicked(View view)
	{
		Mode(ERASE);
		showToast(this.getString(R.string.erase_string), Toast.LENGTH_LONG);
	}

	public void drawButtonClicked(View view)
	{
		showDialog(DIAGLOG_CHOOSE_ROUTE_ID);
		Mode(DRAW);
	}

	public void moveButtonClicked(View view)
	{
		Mode(MOVE);
		showToast(this.getString(R.string.move_string), Toast.LENGTH_LONG);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id)
		{
		case DIALOG_BACK_ID:
			builder.setMessage(getString(R.string.back_press_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
									startActivity(new Intent(
											OffenseModeActivity.this,
											MainActivity.class));
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									showToast(
											getString(R.string.press_menu_string),
											Toast.LENGTH_LONG);
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_ERASE_ID:
			builder.setMessage(R.string.erase_question)
					.setCancelable(false)
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									Mode(CLEAR);
								}
							}).setNegativeButton(R.string.no, null);
			dialog = builder.create();
			break;
		case DIALOG_SAVE_BEFORE_CHANGE_ID:
			builder.setMessage(R.string.change_question)
					.setCancelable(false)
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									mSavedBeforeChanged = true;
									showDialog(DIALOG_SAVE_ID);
								}
							})
					.setNegativeButton(R.string.no,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									mIntent = new Intent(
											OffenseModeActivity.this,
											DefenseModeActivity.class);
									startActivity(mIntent);
								}
							});
			dialog = builder.create();
			break;
		case DIAGLOG_CHOOSE_ROUTE_ID:
			builder.setCancelable(false)
					.setPositiveButton(R.string.route,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									Route(-1);
								}
							})
					.setNegativeButton(R.string.block,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									Route(BLOCK);
									showToast(mContext
											.getString(R.string.draw_string),
											Toast.LENGTH_LONG);
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_SAVE_ID:
			Context context = getApplicationContext();
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(LAYOUT_INFLATER_SERVICE);
			final View layout = inflater.inflate(R.layout.save_dialog, null);

			builder.setView(layout);
			builder.setPositiveButton(R.string.ok,
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int id)
						{
							mPlayNameSave = (EditText) layout
									.findViewById(R.id.playNameSave);
							mPlay = OffenseModeView.PlayToSave();

							showToast(mContext.getString(R.string.saving),
									Toast.LENGTH_SHORT);

							if (mPlayNameSave != null)
								mPlay.Name(mPlayNameSave.getText().toString());
							else
								mPlay.Name("new");

							Runnable runnable = new Runnable()
							{
								public void run()
								{
									mUtil.SavePlay(mPlay,
											OffenseModeView.Bitmap(),
											OffenseModeView.Paint(),
											OffenseModeView.Width(),
											OffenseModeView.Height());

									mPlay.Saved(true);
									mHandler.post(new Runnable()
									{
										public void run()
										{
											showToast(mContext
													.getString(R.string.saved),
													Toast.LENGTH_SHORT);
										}
									});
								}
							};
							new Thread(runnable).start();

							if (mSavedBeforeChanged == true)
							{
								mIntent = new Intent(OffenseModeActivity.this,
										DefenseModeActivity.class);
								startActivity(mIntent);
							}
						}
					});
			builder.setNegativeButton(R.string.cancel,
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int id)
						{
							mPlay.Saved(false);
						}
					});
			dialog = builder.create();
			break;
		}
		return dialog;
	}

	private void showToast(String message, int duration)
	{
		mToast = Toast.makeText(getApplicationContext(), message, duration);

		mToast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
		mToast.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.offense_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.change_defense:
			if (mPlay.Saved() == true)
			{
				mIntent = new Intent(OffenseModeActivity.this,
						DefenseModeActivity.class);
				startActivity(mIntent);
			} else
				showDialog(DIALOG_SAVE_BEFORE_CHANGE_ID);
			return true;
		case R.id.preview_mode:
			onPreviewModeMenuItemClicked();
			return true;
		case R.id.clear_offense:
			showDialog(DIALOG_ERASE_ID);
			return true;
		case R.id.save_offense:
			showDialog(DIALOG_SAVE_ID);
			return true;
		}
		return false;
	}

	protected void onPreviewModeMenuItemClicked()
	{
		mPlay = OffenseModeView.PlayToView();
		PreviewModeView.PlayToView(mPlay);
		PreviewModeView.PlayType(OFFENSE);
		startActivity(new Intent(this, PreviewModeActivity.class));
	}
}